package test.Ejercicio3;

import impl.AbstractSorter;
import impl.SelectionSorter;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<Persona> nameListA = new ArrayList<Persona>();
        System.out.println("Lista sin ordenar:");
        addPeople(nameListA);
        List<Persona> nameListB = new ArrayList<Persona>(nameListA);
        printList(nameListA);
        AbstractSorter sorter = new SelectionSorter();

        sortSurname(nameListA, sorter);
        sortName(nameListB, sorter);
    }

    private static void sortName(List<Persona> nameListB, AbstractSorter sorter) {
        sorter.sort(new nameComparator(), nameListB);
        System.out.println("Lista ordenada por nombre y apellidos:");
        printList(nameListB);
    }

    private static void sortSurname(List<Persona> nameListA, AbstractSorter sorter) {
        sorter.sort(new surnameComparator(), nameListA);
        System.out.println("Lista ordenada por apellido y nombre:");
        printList(nameListA);
    }

    private static void addPeople(List<Persona> nameListA) {
        nameListA.add(new Persona("Alonso", "Paula"));
        nameListA.add(new Persona("Diaz", "Diego"));
        nameListA.add(new Persona("Alonso", "Diego"));
    }

    private static void printList(List<Persona> list) {
        for (Persona aList : list) {
            System.out.println(aList.getSurname() + ", " + aList.getName());
        }
        System.out.println("\n\n");
    }

    private static class surnameComparator implements Comparator<Persona> {
        public int compare(Persona s1, Persona s2) {
            if (s1.getSurname().equalsIgnoreCase(s2.getSurname())) {
                return s1.getName().compareToIgnoreCase(s2.getName());
            } else {
                return s1.getSurname().compareToIgnoreCase(s2.getSurname());
            }
        }
    }

    private static class nameComparator implements Comparator<Persona> {
        public int compare(Persona s1, Persona s2) {
            if (s1.getName().equalsIgnoreCase(s2.getName())) {
                return s1.getSurname().compareToIgnoreCase(s2.getSurname());
            } else {
                return s1.getName().compareToIgnoreCase(s2.getName());
            }
        }
    }
}